package com.example.imgrecognition;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.imgrecognition.Activity.TakePhotoActivity;
import com.example.imgrecognition.Util.ToastUtil;
import com.example.imgrecognition.service.StateService;
import com.zk.code.net.NetUtil;

import java.io.File;
import java.util.ArrayList;

public class MainActivity extends Activity {

    private String[] mNames = {"万磁王", "恩佐斯", "加拉克苏斯大王", "死亡之翼", "伊兰尼库斯"};
    private int[] mImgs = {R.mipmap.a, R.mipmap.b, R.mipmap.c, R.mipmap.d, R.mipmap.e};
    private final static int IMG_RECOGNITION = 0;
    private ImageView iv;
    private ArrayList<String> arrlist;
    public  boolean state = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        state = getIntent().getBooleanExtra("state",false);
        iv = (ImageView) findViewById(R.id.iv);
        arrlist = getIntent().getStringArrayListExtra("list");
        findViewById(R.id.bt).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, ImgRecognitionActivity.class);
                startActivityForResult(intent, IMG_RECOGNITION);
            }
        });
       /* Intent intent = new Intent();
        intent.setClass(MainActivity.this, StateService.class);
        startService(intent);*/


    }

    @Override
    protected void onStart() {
        super.onStart();

    }

    @Override
    protected void onResume() {
        super.onResume();
        if(state == true){
            startPicReg();
            ToastUtil.show(MainActivity.this,"调用了一次");
            state = false;
        }
        ToastUtil.show(MainActivity.this,"先调用了哪里");

    }

    @Override
    protected void onStop() {
        super.onStop();
        state = false;
    }

    private void startPicReg(){
        Intent intent = new Intent(MainActivity.this, ImgRecognitionActivity.class);
        intent.putExtra("list",arrlist);
        state = false;
        startActivityForResult(intent, IMG_RECOGNITION);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        state = false;
        Toast.makeText(MainActivity.this,"调用了这里。。。heiheihei",Toast.LENGTH_LONG).show();
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == IMG_RECOGNITION) {
            //ToastUtil.show(MainActivity.this,"调用了这里。。。11");
            Toast.makeText(MainActivity.this,"调用了这里。。。11",Toast.LENGTH_LONG).show();
            state = false;
            int data2 = data.getIntExtra("data", -1);
            if(data2!=-1){
                NetUtil.sendAlarm();
            }
            if(arrlist!=null){
                iv.setImageURI(Uri.fromFile(new File(arrlist.get(data2))));
                Log.e("picaddress",arrlist.get(data2));
            }
            finish();
        }else{
            Toast.makeText(MainActivity.this,"调用了这里。。。22",Toast.LENGTH_LONG).show();

            finish();
        }

       /* Intent intent = new Intent(MainActivity.this, TakePhotoActivity.class);
        startActivity(intent);
*/
    }
}
